import { Component, Injector, ViewEncapsulation, ViewChild, OnInit } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import {  TechDocMaterialEntity, PipelineServiceProxy, PipelineListDto, RequirementMaterialListDto, MaterialChargeServiceProxy, PipelineNodesServiceProxy, InventoryOfMaterialListDto, ReserveMaterialOutput, ReserveMaterialInputDto } from '@shared/service-proxies/service-proxies';
import { finalize, count } from 'rxjs/operators';
import { LazyLoadEvent, Paginator } from 'primeng/primeng';
import { Table } from 'primeng/components/table/table';

@Component({
    selector: 'yl1',
    templateUrl: './yl1.component.html',
    styleUrls: ['./yl1.component.less'],
    encapsulation: ViewEncapsulation.None
})

export class YL1ModalComponent extends AppComponentBase implements OnInit {

    @ViewChild('paginator') paginator: Paginator;
    @ViewChild('dataTable') dataTable: Table;

    bills = new Array<RequirementMaterialListDto>();
    subTaskCode = '';
    drawingCode = '';

    saving = false;

    filterText = '';

    pipe: PipelineListDto = new PipelineListDto();

    charges = new Array<InventoryOfMaterialListDto>();

    outs = new Array<ReserveMaterialOutput>();
    materials = new Array<TechDocMaterialEntity>();
    //table cols
    cols: any[];
    selectedColumns: any[];
    expandedRowKeys = [];

    constructor(
        injector: Injector,
            private _pipelineService: PipelineServiceProxy,
         private _materialChargeServiceProxy: MaterialChargeServiceProxy,
         private _pipelineNodeServiceProxy: PipelineNodesServiceProxy,
    ) {
        super(injector);
    }

    ngOnInit(): void {
        this.cols = [
            {
                field: 'requestedTaskNum',
                header: '任务号',
                width: '150px',
                isDefaultSelect: true
            },
            {
                field: 'requestedDrawingNum',
                header: '图号',
                width: '120px',
                isDefaultSelect: true
            },
            {
                field: 'displayName',
                header: '名称',
                width: '100px',
                isDefaultSelect: true
            },
            { field: 'furnaceBatch', header: '炉批号', width: '100px' },
            {
                field: 'invCode',
                header: '质检编号',
                width: '100px',
                isDefaultSelect: true
            },
            {
                field: 'materialModel',
                header: '牌号',
                width: '100px',
                isDefaultSelect: true
            },
            {
                field: 'certificateNo',
                header: '合格证',
                width: '100px',
                isDefaultSelect: true
            },
            { field: 'materialCode', header: '物质编码', width: '100px' },
            { field: 'materialStatus', header: '材料状态', width: '100px' },
            { field: 'specification', header: '规格', width: '100px' },
            { field: 'size', header: '尺寸', width: '100px' },
            { field: 'unit', header: '计量单位', width: '100px' },
            { field: 'techCondition', header: '技术状态', width: '100px' }
        ];
        this.selectedColumns = this.cols.filter(el => {
            return el.isDefaultSelect;
        });

    }
    getgy(event?: LazyLoadEvent){
        this._pipelineService.getTechDocDto(this.pipe.id).subscribe(result => {
            this.materials = result;
            console.log(this.materials);
        });
    }
    getOuts() {
        this._materialChargeServiceProxy.getReserveMaterial(this.pipe.id)
            .subscribe(result => {
                this.outs = result;
            });
    }
    getCharges(event?: LazyLoadEvent) {
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);
            return;
        }

        this.primengTableHelper.showLoadingIndicator();

        this._materialChargeServiceProxy
            .getMaterialCharges(
                this.filterText,
                1,
                51,
                'id desc',
                this.primengTableHelper.getSkipCount(this.paginator, event),
                5
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.charges = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
    load(pipe: PipelineListDto): void {
        this.pipe = pipe;
        this.getOuts();
    }

      showDialogToAdd() {
        // this.newCar = true;
        var selected = new ReserveMaterialOutput();
        selected.materialId="请填写合格证号";
        selected.itemAmount=0;
        this.outs.push(selected);
      }
      save(): void {

        if (this.outs.length<=0) {
            this.notify.error('预定数量不能为0');
            return;
        }

        this.saving = true;

        var list=new Array<ReserveMaterialInputDto>();
        this.outs.forEach(item => {
            var rmi=new ReserveMaterialInputDto();
            rmi.id=item.id;
            rmi.itemAmount=item.itemAmount;
            rmi.materialId=item.materialId;
            rmi.pipelineEntityId=this.pipe.id
            list.push(rmi);
        });
        this._materialChargeServiceProxy.reserveMaterial(list)
            .pipe(finalize(() => { this.saving = false; }))
            .subscribe(result => {
                this.notify.success('预定成功');
            });
    }

}
